//
//  StringExtensionCXHelp.swift
//  CXSwiftDemo
//
//  Created by 潮汐 on 2023/12/7.
//
import Foundation

extension String {
    ///根据宽度跟字体，计算文字的高度
    static func textAutoHeight(text:String, width:CGFloat, font:UIFont) ->CGFloat{
        
        let string = text as NSString//转化
        let origin = NSStringDrawingOptions.usesLineFragmentOrigin
        let lead = NSStringDrawingOptions.usesFontLeading
        let ssss = NSStringDrawingOptions.usesDeviceMetrics
        let rect = string.boundingRect(with:CGSize(width: width, height:0), options: [origin,lead,ssss], attributes: [NSAttributedString.Key.font:font], context:nil)
        return rect.height
        
    }
    
    ///根据高度跟字体，计算文字的宽度
    static func textAutoWidth(text:String, height:CGFloat, font:UIFont) ->CGFloat{
        
        let string = text as NSString//转化
        let origin = NSStringDrawingOptions.usesLineFragmentOrigin
        let lead = NSStringDrawingOptions.usesFontLeading
        let rect = string.boundingRect(with:CGSize(width:0, height: height), options: [origin,lead], attributes: [NSAttributedString.Key.font:font], context:nil)
        return rect.width
    }
}

